import request from '@/utils/request'
import { UserResponseData, User, AllRoleResponseData, RoleData } from './type'

enum USER_ACL_API_URL {
  /**
   * 获取管理用户分页列表地址
   */
  GET_USER_URL = '/admin/acl/user',

  /**
   * 添加账号接口地址
   */
  ADD_USER_URL = '/admin/acl/user/save',

  /**
   * 修改账号接口地址
   */
  UPDATE_USER_URL = '/admin/acl/user/update',

  /**
   * 获取全部职位和当前账号已有职位信息接口地址
   */
  GET_ALL_ROLE_URL = '/admin/acl/user/toAssign',

  /**
   * 给已有的用户分配角色接口地址
   */
  SET_ROLE_URL = '/admin/acl/user/doAssignRole',

  /**
   * 删除单个用户的接口地址
   */
  DELETE_USER_URL = '/admin/acl/user/remove',

  /**
   * 删除多个用户的接口地址
   */
  DELETE_USERS_URL = '/admin/acl/user/batchRemove',

  /**
   * 搜索用户信息的接口地址
   */
  SEARCH_USERS_URL = '',
}

/**
 * 获取管理用户分页列表接口
 * @param pageNo
 * @param limit
 * @param username
 * @returns Promise<UserResponseData>
 */
export const reqGetUser = (
  pageNo: number,
  limit: number,
  username?: string,
): Promise<UserResponseData> =>
  request.get<any, UserResponseData>(
    USER_ACL_API_URL.GET_USER_URL + `/${pageNo}/${limit}?username=${username}`,
  )

/**
 * 添加/修改账号接口
 * @param data
 * @returns Promise<any>
 */
export const reqAddOrUpdateUser = (data: User): Promise<any> => {
  if (data.id) {
    return request.put<any, any>(USER_ACL_API_URL.UPDATE_USER_URL, data)
  } else {
    return request.post<any, any>(USER_ACL_API_URL.ADD_USER_URL, data)
  }
}

/**
 * 获取全部职位和当前帐号已有职位信息的接口
 * @param userId
 * @returns Promise<AllRoleResponseData>
 */
export const reqAllRole = (userId: number): Promise<AllRoleResponseData> =>
  request.get<any, AllRoleResponseData>(USER_ACL_API_URL.GET_ALL_ROLE_URL + `/${userId}`)

/**
 * 给已有用户分配角色接口
 * @param data
 * @returns Promise<any>
 */
export const reqSetRole = (data: RoleData): Promise<any> =>
  request.post<any, any>(USER_ACL_API_URL.SET_ROLE_URL, data)

/**
 * 删除单个用户的接口
 * @param userId
 * @returns Promise<any>
 */
export const reqDeleteUser = (userId: number): Promise<any> =>
  request.delete<any, any>(USER_ACL_API_URL.DELETE_USER_URL + `/${userId}`)

/**
 * 删除多个用户的接口
 * @param idList
 * @returns Promise<any>
 */
export const reqDeleteUsers = (idList: number[]): Promise<any> =>
  request.delete<any, any>(USER_ACL_API_URL.DELETE_USERS_URL, { data: idList })
